package com.chenglu.android.ordercomment.mvp.presenter;



import android.accounts.NetworkErrorException;


import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.bean.CommonDataBean;
import com.chenglu.android.mine.bean.PutFileBean;
import com.chenglu.android.ordercomment.mvp.contract.OrderCommentContract;
import com.chenglu.android.ordercomment.mvp.model.OrderCommentModel;

import java.io.File;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.RequestBody;


/**
 * @author: lyj
 * @Created by Copyright 2023/3/2 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class OrderCommentPresenter extends BasePresenter<OrderCommentContract.View> implements OrderCommentContract.OrderCommentPresenter {

     private  OrderCommentContract.OrderCommentModel model;

    public OrderCommentPresenter(){
        model = new OrderCommentModel();
    }

    @Override
    public void getOrderComment(String token, RequestBody requestBody) {

        if(!isViewAttached()){
            return;
        }

        model.getOrderComment(token,requestBody)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CommonDataBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CommonDataBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getOrderComment(bean);
                                break;
                            case Constant.HTTPCODE400:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }



    @Override
    public void getPutFile(String token, File file) {
        if(!isViewAttached()){
            return;
        }
        model.getPutFile(token,file)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<PutFileBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull PutFileBean bean) {
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getPutFileSuccess(bean);
                        }else if(bean.getCode()==(Constant.HTTPCODE400)) {
                            ToastUtils.show(bean.getMsg());
                        }else if(bean.getCode()==(Constant.HTTPCODE401)) {
                            ToastUtils.show(bean.getMsg());
                        }else if(bean.getCode()==(Constant.HTTPCODE404)) {
                            ToastUtils.show(bean.getMsg());
                        }else if(bean.getCode()==(Constant.HTTPCODE500)) {
                            ToastUtils.show(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        mView.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

}
